﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business;
using System.Configuration;
using RVEAd.LiveClient.Controls;

namespace RVEAd.LiveClient
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();


            this.Load += new EventHandler(MainForm_Load);
            this.dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);

            loadDataTimer.Interval = (int)(new TimeSpan(0, 0, 10).TotalMilliseconds);
            loadDataTimer.Enabled = true;
        }

        void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            RefreshPanelDate();
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            
            dateTimePicker1.Value = DateTime.Now;//.AddDays(-29);
            
            ShowIntervalControl();

        }

        private void ShowIntervalControl()
        {
            if (DateTime.Now.Hour > 12)
            {
                // PM
                PutControl(false);
            }
            else
            {
                // AM
                PutControl(true);
            }
        }

        private void PutControl(bool isAM)
        {
            panel1.Controls.Clear();

            if (isAM)
            {
                var amPanel = new LiveClient.Controls.AMPanel();
                amPanel.Date = dateTimePicker1.Value;

                amPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
                panel1.Controls.Add(amPanel);
            }
            else
            {
                var pmPanel = new LiveClient.Controls.PMPanel();
                pmPanel.Date = dateTimePicker1.Value;

                pmPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
                panel1.Controls.Add(pmPanel);

            }
        }

        private void RefreshPanelDate()
        {
            if (panel1.HasChildren)
            {
                if (panel1.Controls[0] is Controls.AMPanel)
                {
                    var control = ((AMPanel)panel1.Controls[0]);
                    control.Date = dateTimePicker1.Value;
                    control.LoadData();
                }
                else if (panel1.Controls[0] is Controls.PMPanel)
                {
                    var control = ((PMPanel)panel1.Controls[0]);
                    control.Date = dateTimePicker1.Value;
                    control.LoadData();
                }
            }
        }

        private void clientTimer_Tick(object sender, EventArgs e)
        {
            lblTimer.Text = DateTime.Now.ToString("hh:mm:ss");
        }

        private void loadDataTimer_Tick(object sender, EventArgs e)
        {
            RefreshPanelDate();
        }

        private void btnShowAM_Click(object sender, EventArgs e)
        {
            PutControl(true);
        }

        private void btnShowPM_Click(object sender, EventArgs e)
        {
            PutControl(false);
        }
    }
}
